<html>
<head><meta charset="utf-8"><title>generic argument disambiguation · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html">generic argument disambiguation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177663600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177663600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jplatte <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177663600">(Oct 08 2019 at 22:02)</a>:</h4>
<p>Hi, I've started work on disambiguation of identifiers as generic arguments between regular type generics and const generics. <span class="user-mention" data-user-id="121053">@varkor</span>  you wrote a comment <a href="https://github.com/rust-lang/rust/issues/60804#issuecomment-492011332" target="_blank" title="https://github.com/rust-lang/rust/issues/60804#issuecomment-492011332">here</a> where you outlined a rough approach to this. I've now completed most of the work in libsyntax, but in HIR lowering I don't know what addition information I might have there that allows disambiguating at that stage. Could you give me some pointers?</p>



<a name="177663671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177663671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jplatte <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177663671">(Oct 08 2019 at 22:03)</a>:</h4>
<p>What I've done so far is at up at <a href="https://github.com/jplatte/rust/tree/generic-arg-disambiguation" target="_blank" title="https://github.com/jplatte/rust/tree/generic-arg-disambiguation">https://github.com/jplatte/rust/tree/generic-arg-disambiguation</a>, in case anyone wants to look at it.</p>



<a name="177689023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177689023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177689023">(Oct 09 2019 at 07:31)</a>:</h4>
<p><span class="user-mention" data-user-id="198047">@jplatte</span> <br>
AST changes are unnecessary, see the later comment - <a href="https://github.com/rust-lang/rust/issues/60804#issuecomment-516769465" target="_blank" title="https://github.com/rust-lang/rust/issues/60804#issuecomment-516769465">https://github.com/rust-lang/rust/issues/60804#issuecomment-516769465</a></p>



<a name="177689113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177689113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177689113">(Oct 09 2019 at 07:33)</a>:</h4>
<p>Everything can be done in resolve (visit_generic_arg) and AST -&gt; HIR lowering (lower_generic_arg).</p>



<a name="177829718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177829718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jplatte <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177829718">(Oct 10 2019 at 16:53)</a>:</h4>
<p>Does it make sense to do it that way though? The type variant can represent a bunch of stuff beyond just identifiers that aren't allowed for const generics. Filtering those out from the value resolution fallback seems more error-prone than distinguishing those cases when parsing.</p>



<a name="177834514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177834514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177834514">(Oct 10 2019 at 17:45)</a>:</h4>
<p>AST preserves all the data necessary to discern between an <code>ident</code> and an arbitrary path, it's <code>segments.len() == 1 &amp;&amp; segments[0].arg.is_none()</code>, not too error prone.<br>
The alternative is to make two variants that are supposed to behave identically almost everywhere - keeping them synchronized does seem more error prone (and taking more code).</p>



<a name="177834608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177834608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177834608">(Oct 10 2019 at 17:46)</a>:</h4>
<p><code>Pat::Ident</code> is separate from <code>Path::Path</code> because it also covers things like <code>ident @ PAT</code> or <code>mut ident</code>, otherwise it would also make sense to merge them.</p>



<a name="177834867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177834867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177834867">(Oct 10 2019 at 17:49)</a>:</h4>
<blockquote>
<p>it's segments.len() == 1 &amp;&amp; segments[0].arg.is_none(), not too error prone.</p>
</blockquote>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> maybe abstract this tho? probably used in other places</p>



<a name="177835401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177835401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177835401">(Oct 10 2019 at 17:54)</a>:</h4>
<p>If it's used multiple times, then sure, why not.</p>



<a name="177845320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177845320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jplatte <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177845320">(Oct 10 2019 at 19:29)</a>:</h4>
<p>But <code>segments.len() == 1 &amp;&amp; segments[0].arg.is_none()</code> would be pretty restrictive, no? Why should e.g. (associated) paths to constants not be disambiguated too?</p>



<a name="177847619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177847619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177847619">(Oct 10 2019 at 19:56)</a>:</h4>
<p>We need to disambiguate in favor of types (for backward compatibility), and for multi-segment paths type-based resolutions are unavailable if we are disambiguating before/during lowering to HIR.<br>
So it may happen that the path resolves to a value but not a type during early resolution, but resolves to a type during type-based resolution.</p>



<a name="177847818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177847818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177847818">(Oct 10 2019 at 19:58)</a>:</h4>
<p>If the disambiguation is delayed until the last moment (generic argument substitution), then we can disambiguate multi-segment paths.</p>



<a name="177848002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177848002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177848002">(Oct 10 2019 at 20:00)</a>:</h4>
<p>Hm, the <code>arg.is_none()</code> condition should be unnecessary though.</p>



<a name="177862491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/177862491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jplatte <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#177862491">(Oct 10 2019 at 23:09)</a>:</h4>
<p>Okay, so that means disambiguation has to (partially?) happen later, right? But I don't need to introduce new <code>GenericArg</code> variants for that? Just some changes to resolve and lowering in libsyntax and some other things later? I can try making that work :)</p>



<a name="178604925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/178604925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#178604925">(Oct 20 2019 at 18:44)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> I am trying to pick this up from <span class="user-mention" data-user-id="198047">@jplatte</span> and I just want to make sure I am understanding what you were suggesting. If I am reading everything correctly, you are suggesting passing a path through <code>ast::GenericArgs</code> that could either be a type or a constexpr. Once we hit AST -&gt; HIR we check for <code>segments.len() == 1 &amp;&amp; segments[0].arg.is_none()</code> to check if it is an <code>ident</code>, and from there check if it is a type or const argument, in that order. Is that all correct or am I completely off in the weeds?</p>



<a name="178614732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/178614732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#178614732">(Oct 20 2019 at 23:13)</a>:</h4>
<p>I think the idea is that a const parameter would initially be a <code>GenericArg::Type</code> with a single segment and no arguments — when we try to resolve the path in the type namespace, if it doesn't exist, then we also try in the value namespace — and if that succeeds, we lower to a const argument instead</p>



<a name="178614779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/178614779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#178614779">(Oct 20 2019 at 23:14)</a>:</h4>
<p>which I think is what you're describing</p>



<a name="178618378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/generic%20argument%20disambiguation/near/178618378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/generic.20argument.20disambiguation.html#178618378">(Oct 21 2019 at 01:11)</a>:</h4>
<p>ok, that's more or less what I was thinking, though coming at it from a slightly different direction</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>